home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifgate / areas.c next >
Encoding:
C/C++ Source or Header  |  1994-04-04  |  3.5 KB  |  196 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "lutil.h"
  6. #include "xutil.h"
  7. #include "areas.h"
  8. #include "config.h"
  9.  
  10. extern area_list *badgroups;
  11. areang_list *nglist;
  12. char *defgroup=NULL,*defdist=NULL,*defarea=NULL;
  13.  
  14. static char *expand(char*,char*);
  15.  
  16. void tidy_arealist(al)
  17. area_list *al;
  18. {
  19.     area_list *tmp;
  20.  
  21.     for (;al;al=tmp)
  22.     {
  23.         tmp=al->next;
  24.         if (al->name) free(al->name);
  25.         free(al);
  26.     }
  27. }
  28.  
  29. area_list *areas(ngroups)
  30. char *ngroups;
  31. {
  32.     char *ng,*p,*q,*r;
  33.     area_list *start=NULL,**cur,*bg;
  34.     areang_list *ngl;
  35.     int forbid=0;
  36.  
  37.     if (ngroups == NULL) return NULL;
  38.     ng=xstrcpy(ngroups);
  39.     cur=&start;
  40.  
  41.     for (p=strtok(ng," ,\t\n");p;p=strtok(NULL," ,\t\n"))
  42.     {
  43.         q=NULL;
  44.         for (bg=badgroups;bg;bg=bg->next)
  45.         if (!strncmp(bg->name,p,strlen(bg->name)))
  46.         {
  47.             loginf("Message not gated due to forbidden group %s",p);
  48.             forbid=1;
  49.             break;
  50.         }
  51.         for (ngl=nglist;ngl;ngl=ngl->next)
  52.         if (!strcmp(ngl->ng,p))
  53.         {
  54.             q=ngl->area;
  55.             break;
  56.         }
  57.         if (q || defarea)
  58.         {
  59.             if (q == NULL)
  60.             {
  61.                 for (r=p;*r;r++) *r=toupper(*r);
  62.                 q=expand(defarea,p);
  63.             }
  64.             debug(9,"newsgroup \"%s\" -> area \"%s\"",q);
  65.             (*cur)=(area_list *)xmalloc(sizeof(area_list));
  66.             (*cur)->next=NULL;
  67.             (*cur)->name=xstrcpy(q);
  68.             cur=&((*cur)->next);
  69.         }
  70.     }
  71.  
  72.     free(ng);
  73.     if (forbid)
  74.     {
  75.         tidy_arealist(start);
  76.         return NULL;
  77.     }
  78.     else return start;
  79. }
  80.  
  81. void ngdist(area,newsgroup,distribution)
  82. char *area,**newsgroup,**distribution;
  83. {
  84.     areang_list *ang;
  85.     char *p,*q;
  86.  
  87.     *newsgroup=NULL;
  88.     *distribution=NULL;
  89.     p=xstrcpy(area);
  90.     if ((*(q=p+strlen(p)-1) == '\r') || (*q == '\n')) *q='\0';
  91.  
  92.     for (ang=nglist;ang;ang=ang->next)
  93.     if (strcasecmp(p,ang->area) == 0)
  94.     {
  95.         *newsgroup=ang->ng;
  96.         *distribution=ang->dist;
  97.         debug(9,"area \"%s\" -> newsgroup \"%s\", dist \"%s\"",
  98.             p,*newsgroup,*distribution);
  99.         break;
  100.     }
  101.  
  102.     if ((*newsgroup == NULL) && defgroup)
  103.     {
  104.         for (q=p;*q;q++) *q=tolower(*q);
  105.         *newsgroup=expand(defgroup,p);
  106.         *distribution=defdist;
  107.         debug(9,"area \"%s\" -> default newsgroup \"%s\", dist \"%s\"",
  108.             p,*newsgroup,*distribution);
  109.     }
  110.  
  111.     if (*newsgroup == NULL)
  112.     {
  113.         loginf("No newsgroup for area tag %s",p);
  114.     }
  115.  
  116.     free(p);
  117. }
  118.  
  119. void readareas(fn)
  120. char *fn;
  121. {
  122.     areang_list **cur;
  123.     FILE *fp;
  124.     char buf[128],*p,*q,*r;
  125.  
  126.     cur=&nglist;
  127.     if (defgroup) free(defgroup);
  128.     defgroup=NULL;
  129.     if (defdist) free(defdist);
  130.     defdist=NULL;
  131.     if (defarea) free(defarea);
  132.     defarea=NULL;
  133.  
  134.     if ((fp=fopen(fn,"r")) == NULL)
  135.     {
  136.         logerr("$unable to open area file %s",fn);
  137.         return;
  138.     }
  139.     while (fgets(buf,sizeof(buf)-1,fp))
  140.     {
  141.         if ((p=strchr(buf,'#'))) *p='\0';
  142.         p=strtok(buf," \t\n");
  143.         q=strtok(NULL," \t\n");
  144.         r=strtok(NULL," \t\n");
  145.         if (p && q)
  146.         {
  147.             if (strcmp(q,"*") == 0)
  148.             {
  149.                 debug(9,"adding default areatag \"%s\"",p);
  150.                 defarea=xstrcpy(p);
  151.             }
  152.             if (strcmp(p,"*") == 0)
  153.             {
  154.                 debug(9,"adding default group \"%s\" (dist %s)",
  155.                     q,r);
  156.                 defgroup=xstrcpy(q);
  157.                 defdist=xstrcpy(r);
  158.             }
  159.             if ((strcmp(p,"*") != 0) && (strcmp(p,"*") != 0))
  160.             {
  161.                 debug(9,"adding area \"%s\" for ng \"%s\" (dist %s)",
  162.                     p,q,r);
  163.                 (*cur)=(areang_list *)
  164.                     xmalloc(sizeof(areang_list));
  165.                 (*cur)->next=NULL;
  166.                 (*cur)->area=xstrcpy(p);
  167.                 (*cur)->ng=xstrcpy(q);
  168.                 if (r) (*cur)->dist=xstrcpy(r);
  169.                 else (*cur)->dist=NULL;
  170.                 cur=&((*cur)->next);
  171.             }
  172.         }
  173.     }
  174.     fclose(fp);
  175. }
  176.  
  177. char *expand(p,q)
  178. char *p,*q;
  179. {
  180.     char *r,*tmp,*ret;
  181.  
  182.     debug(9,"expanding \"%s\" + \"%s\"",p,q);
  183.  
  184.     tmp=xstrcpy(p);
  185.     if ((r=strchr(tmp,'*'))) *r++='\0';
  186.     ret=xstrcpy(tmp);
  187.     if (r)
  188.     {
  189.         ret=xstrcat(ret,q);
  190.         ret=xstrcat(ret,r);
  191.     }
  192.     free(tmp);
  193.     debug(9,"expand result \"%s\"",ret);
  194.     return ret;
  195. }
  196.